package com.socket.nio.nio.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

/**
 * Bytebuf 输出信息工具类
 * @author Hexudong
 * @date 2025/4/12
 */
public class ByteBufPrinter {

  /**
   * 以友好的格式打印 ByteBuf 内容
   * @param buf 要打印的 ByteBuf
   */
  public static void prettyPrint(ByteBuf buf) {
    if (buf == null) {
      System.out.println("ByteBuf is null");
      return;
    }
    System.out.println("ByteBuf info:");
    System.out.println("  Readable bytes: " + buf.readableBytes());
    System.out.println("  Reader index: " + buf.readerIndex());
    System.out.println("  Writer index: " + buf.writerIndex());
    System.out.println("  Capacity: " + buf.capacity());
    System.out.println("  Hex dump:");

    // 使用 Netty 提供的工具类进行十六进制转储
    System.out.println(ByteBufUtil.prettyHexDump(buf));
  }
}
